#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include<fcntl.h>
#include <zconf.h>
 #include <arpa/inet.h>

       #define MY_SOCK_PATH "/somepath"
       #define LISTEN_BACKLOG 50
       #define PORT 3333       // 服务器监听端口
       #define MAX_LEN 1024    // 接收浏览器数据

       #define handle_error(msg) \
           do { perror(msg); exit(EXIT_FAILURE); } while (0)

       int
       main(int argc, char *argv[])
       {
           int sfd, cfd;
           int reuse = 1;
           int i = 0;
           struct sockaddr_in server_addr;   // 地址结构体
           char buf[MAX_LEN];
           char* bufReply = "+$#00";
           
           
           memset(&server_addr, 0, sizeof(server_addr));
           server_addr.sin_family = AF_INET;
           server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
           server_addr.sin_port = htons(PORT);
           
           
           sfd = socket(AF_INET, SOCK_STREAM, 0);
           if (sfd == -1)
               handle_error("socket");
           setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (const char*)& reuse, sizeof(int));


           if (bind(sfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1)
               handle_error("bind");

           if (listen(sfd, LISTEN_BACKLOG) == -1)
               handle_error("listen");

            /* Now we can accept incoming connections one
              at a time using accept(2) */
              cfd = accept(sfd, NULL, NULL);
              printf("gdb connect ok \n");

           while(1)
           {
               if (cfd == -1)
                   handle_error("accept");
               i = read(cfd, buf, MAX_LEN);
               //printf("%d\n", i);
               buf[i] = '\0';
               printf("->%s\n", buf);
               
               if(buf[0] == '+')
               {
                   write(cfd, bufReply, sizeof(bufReply));
                   printf("<-%s\n", bufReply);
               }
           }
           
           
           close(cfd);
           close(sfd);
           

           /* Code to deal with incoming connection(s)... */

           /* When no longer required, the socket pathname, MY_SOCK_PATH
              should be deleted using unlink(2) or remove(3) */
              
           
       }

